package com.xqls.controller;

import com.alibaba.fastjson.JSONObject;
import com.xqls.bean.AdministrationBean;
import com.xqls.biz.FluggasBlockAdministrationBiz;
import com.xqls.biz.FluggasTableFieldBiz;
import com.xqls.dal.model.FluggasBlockAdministration;
import com.xqls.dal.model.FluggasTableField;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hdg on 2017/7/6.
 */
@Controller
@RequestMapping(value = "/fluggasadministration")
public class FluggasBlockAdministrationController {

    @Autowired
    FluggasBlockAdministrationBiz FluggasBlockAdministrationBiz;
    @Autowired
    FluggasTableFieldBiz FluggasTableFieldBiz;
    @RequestMapping(value = "/fluggasadministrationList")
    public ModelAndView fluggasadministrationList() {
        ModelAndView view = new ModelAndView("fluggasadministration/fluggasadministrationList");
        view.addObject("stringUtils", new StringUtils());
        return view;
    }

    @RequestMapping(value = "/fluggasadministrationListData")
    @ResponseBody
    public CommonResult fluggasadministrationListData(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
        CommonResult result = FluggasBlockAdministrationBiz.selectBlockAdministration(commonPage,request,response);
        if (result.getIsSuccess()) {
            result.setData(commonPage);
        }
        return result;
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public String save(FluggasBlockAdministration bean) {
        CommonResult result = FluggasBlockAdministrationBiz.updateFluggasBlockAdministration(bean);
        return JSONObject.toJSONString(result);
    }

    @RequestMapping(value = "/fluggasmaintainAdministration")
    public ModelAndView fluggasmaintainAdministration(@RequestParam(value = "fluggasid") Integer fluggasid) {
        ModelAndView view = new ModelAndView("fluggasadministration/fluggasmaintainAdministration");
        FluggasBlockAdministration fluggasBlockAdministration = FluggasBlockAdministrationBiz.queryFluggasBlockAdministratiobByfluggas(fluggasid);
        String displayfield=fluggasBlockAdministration.getDisplayfield();
        List<String> fieldList=new ArrayList<String>();
        String[] str=displayfield.split(",");
        for (int i=0;i<str.length;i++){
            fieldList.add(str[i]);
        }
        List<FluggasTableField> TableFieldList = FluggasTableFieldBiz.selectAll();
        List<Map<String,Object>> fields = new ArrayList<Map<String,Object>>();
        for (int i=0 ;i<TableFieldList.size();i++){
            Map<String,Object> map= new HashMap();
            String CE="";
            CE=TableFieldList.get(i).getTableFieldAnnotations()+":"+TableFieldList.get(i).getTableFieldname();
            map.put("administration",CE);
            map.put("fieldId",TableFieldList.get(i).getFieldno());
            fields.add(map);
        }
        view.addObject("allfields",fields);
        view.addObject("allfieldList",fieldList);
        view.addObject("fluggasBlockAdministration", fluggasBlockAdministration);
        return view;
    }

    @RequestMapping("/deletefluggas")
    @ResponseBody
    public String deletefluggas(@RequestBody List<Integer> list){
        CommonResult commonResult = this.FluggasBlockAdministrationBiz.deleteByList(list);
        return JSONObject.toJSONString(commonResult);
    }

    @RequestMapping(value = "/toInsertPage")
    public ModelAndView toInsertPage() {
        ModelAndView view = new ModelAndView("fluggasadministration/addfluggasmaintainAdministration");
        List<FluggasTableField> TableFieldList = FluggasTableFieldBiz.selectAll();
        List<Map<String,Object>> fields = new ArrayList<Map<String,Object>>();
        for (int i=0 ;i<TableFieldList.size();i++){
            Map<String,Object> map= new HashMap();
            String CE="";
            CE=TableFieldList.get(i).getTableFieldAnnotations()+":"+TableFieldList.get(i).getTableFieldname();
            map.put("administration",CE);
            map.put("fieldId",TableFieldList.get(i).getFieldno());
            fields.add(map);
        }
        view.addObject("allfields",fields);
        return view;
    }

    @RequestMapping(value = "/addfluggas")
    @ResponseBody
    public String addfluggas(FluggasBlockAdministration bean){
        CommonResult result  = this.FluggasBlockAdministrationBiz.insertFluggasBlockAdministration(bean);
        return JSONObject.toJSONString(result);
    }

    @RequestMapping(value = "administrationEdit")
    public ModelAndView realdataEdit(@RequestParam(value = "fluggasid") Integer fluggasid) {
        ModelAndView view = new ModelAndView("fluggasadministration/fluggasadministrationEdit");
        view.addObject("Fluggasadministration", FluggasBlockAdministrationBiz.queryFluggasBlockAdministratiobByfluggas(fluggasid));
        return view;
    }

    @RequestMapping(value = "/saveFiled")
    @ResponseBody
    public CommonResult saveFiled(FluggasBlockAdministration bean, String filed, String fieldalias, String type) {

        CommonResult result = FluggasBlockAdministrationBiz.addFiled(filed,type);
        FluggasTableField fluggasTableField = new FluggasTableField();
        fluggasTableField.setTableFieldname(filed);
        fluggasTableField.setTableFieldAnnotations(fieldalias);
        FluggasTableFieldBiz.insert(fluggasTableField);


        return result;

    }
}
